<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/12
 * Time: 18:35
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\common\model\User;
use think\Config;

class Order extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function unionpay()
    {
        $user_id = request()->get('user_id');
        $money   = request()->get('money');
        $user    = User::where('status','normal')->where('id',$user_id)->find();
        if( !$user ){
            $this->error('会员异常~！');
        }
        $this->view->assign('user_id',$user_id);
        $this->view->assign('money',$money);
        return $this->view->fetch();
    }

    public function placeOrder(Request $request)
    {
        $money = $request->post('money');
        $user_id = $request->post('user_id');
        $type  = 'unionpay';
        $banktype = $request->param('banktype');
        $walletService = WalletService::getInstance();
        $res = $walletService->recharge($user_id,$money,$type,$banktype);
        echo '
	<form action="'.$res['url'].'" method="post" name="orderForm">
	<input type="hidden" name="customer"  value="'.$res['data']['customer'].'"> 	
	<input type="hidden" name="bankcode"  value="'.$res['data']['bankcode'].'"> 
	<input type="hidden" name="amount"  value="'.$res['data']['amount'].'">
	<input type="hidden" name="orderid"  value="'.$res['data']['orderid'].'">
	<input type="hidden" name="asynbackurl"  value="'.$res['data']['asynbackurl'].'">
	<input type="hidden" name="request_time"  value="'.$res['data']['request_time'].'">
	<input type="hidden" name="synbackurl"  value="'.$res['data']['synbackurl'].'">
	<input type="hidden" name="israndom"  value="'.$res['data']['israndom'].'">
	<input type="hidden" name="sign"  value="'.$res['data']['sign'].'">
	</form>
	<script type="text/javascript">
	  setTimeout(function(){document.orderForm.submit();},1000)
	</script>
	';
        exit;
    }

    public function paySuccess()
    {
        $OrderId = urldecode($_REQUEST["orderid"]);		//商户系统传入的orderid
        $Result = urldecode($_REQUEST["result"]);		//订单结果 0：支付成功
        $Amount = urldecode($_REQUEST["amount"]);		//订单金额 单位元
        $SourceAmount = urldecode($_REQUEST["sourceamount"]);		//提交金额 单位元
        $PayAmount = urldecode($_REQUEST["payamount"]);		//支付金额 单位元
        $Systemorderid= urldecode($_REQUEST["systemorderid"]);		//此次订单过程中系统内的订单Id
        $Completetime = urldecode($_REQUEST["completetime"]);	//订单时间
        $Sign = urldecode($_REQUEST["sign"]);	//MD5签名

        $Key = Config::get('pay.key'); //支付秘钥
        $sign_str = "orderid=".$OrderId."&result=".$Result."&amount=".$Amount.
            "&systemorderid=".$Systemorderid."&completetime=".$Completetime."&key=".$Key;

        $SignLocal = md5($sign_str);
        $ret2 = ($SignLocal == $Sign) ? '签名验证成功':'签名验证失败';

        $res = [
            'orderid'   => $OrderId,
            'result'    => $Result,
            'amount'    => $Amount,
            'sourceamount'=>$SourceAmount,
            'payamount' => $PayAmount,
            'systemorderid'=>$Systemorderid,
            'completetime'=> $Completetime,
            'sign'      => $Sign,
            'signRes'   => $ret2
        ];

        $this->assign('res',$res);
        return $this->view->fetch();
    }
}